Lift Cookbook by Richard Dallaway

Lift Cookbook by Richard Dallaway

Author:Richard Dallaway [Richard Dallaway]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449362676
Publisher: O’Reilly Media
Published: 2013-06-23T16:00:00+00:00


See Also

Custom 404 Page describes how to create a custom 404 (not found) page.

Streaming Content

Problem

You want to stream content back to the web client.

Solution

Use OutputStreamResponse, passing it a function that will write to the OutputStream that Lift supplies.

In this example, we’ll stream all the integers from one, via a REST service:

package code.rest import net.liftweb.http.{Req,OutputStreamResponse} import net.liftweb.http.rest._ object Numbers extends RestHelper { // Convert a number to a String, and then to UTF-8 bytes // to send down the output stream. def num2bytes(x: Int) = (x + "\n") getBytes("utf-8") // Generate numbers using a Scala stream: def infinite = Stream.from(1).map(num2bytes) serve { case Req("numbers" :: Nil, _, _) => OutputStreamResponse( out => infinite.foreach(out.write) ) } }

Scala’s Stream class is a way to generate a sequence with lazy evaluation. The values being produced by infinite are used as example data to stream back to the client.

Wire this into Lift in Boot.scala:

LiftRules.dispatch.append(Numbers)

Visiting http://127.0.0.1:8080/numbers will generate a 200 status code and start producing the integers from 1. The numbers are produced quite quickly, so you probably don’t want to try that in your web browser, but instead from something that is easier to stop, such as cURL.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.